﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcMusicStore.ViewModels;
using MvcMusicStore.Models;
using LibraryMusicStore;

namespace MvcMusicStore.Controllers
{
    public class StoreController : Controller
    {
        MusicStoreEntities storeDB = new MusicStoreEntities();
        //listing page of the music genre in out music store
        // GET: /Store/

        public ActionResult Index()
        {
            try
            {
                //create list of genres
                //var genres = new List<string> { "rock", "yard", "pop", "country" };

                //retrieve list of genres from database
                var genres = from genre in storeDB.Genres
                             select genre.Name;

                //create out view model
                var viewModel = new StoreIndexViewModel
                {
                    NumberOfGenre = genres.Count(),
                    Genres = genres.ToList()
                };

                return View(viewModel);
            }
            catch (Exception)
            {
                return View("../Shared/Error");
            }
        }

        //browse page that list all of music albums in a particular genre
        // GET: /Store/Browse

        public ActionResult Browse(string genre)
        {
            try
            {
                ViewData["vat"] = 1 + storeDB.VATs.First().Value / 100;
                var genreModel = storeDB.Genres.First(g => g.Name == genre);

                var viewModel = new StoreBrowserViewModel()
                {
                    Genre = genreModel,
                    Albums = genreModel.Albums
                        .Where(a => a.Deleted == false || a.Deleted == null)
                        .OrderByDescending(a => a.AlbumId).ToList()
                };

                return View(viewModel);
            }
            catch (Exception)
            {
                return View("../Shared/Error");
            }
            
        }

        //details page that show infomation about a specific music album
        // GET: /Store/Details

        public ActionResult Details(int id)
        {
            try
            {
                ViewData["vat"] = 1 + storeDB.VATs.First().Value / 100;
                var album = storeDB.Albums.First(a => a.AlbumId == id);

                var receipts = storeDB.Receipts;
                var issues = storeDB.Issues;

                ViewData["Stock"] = Stock.GetStock(album.AlbumId, receipts, issues);

                return View(album);
            }
            catch (Exception)
            {
                return View("../Shared/Error");
            }
        }

        [ChildActionOnly]
        public ActionResult GenreMenu()
        {
            try
            {
                var genre = storeDB.Genres.ToList();

                return View(genre);
            }
            catch (Exception)
            {
                return View("../Shared/Error");
            }
        }
    }
}
